import apiClient, { wrapApiCall } from './api';
import type {
  DomainSummary,
  DomainDetail,
  CreateDomainRequest,
  ApiResponse,
  ListResponse,
} from '../types/api';

// 虚拟机管理API服务
export const domainService = {
  // 获取所有虚拟机, 使用ListResponse包装响应
  listDomains: async (connectionId: string, includeInactive: boolean = true): Promise<DomainSummary[]> => {
    const response = await wrapApiCall<ListResponse<DomainSummary>>(apiClient.get('/domains', {
      params: {
        connection: connectionId,
        inactive: includeInactive
      }
    }));

    if (!response.domains) {
      throw new Error('虚拟机列表响应数据为空');
    }
    
    return response.domains;
  },


  // 获取虚拟机详情
  getDomainDetail: async (connectionId: string, domainName: string): Promise<DomainDetail> => {
    const response = await wrapApiCall<DomainDetail>(apiClient.get('/domain', {
      params: {
        connection: connectionId,
        name: domainName
      }
    }));
    return response;
  },

  // 创建虚拟机
  createDomain: async (connectionId: string, data: CreateDomainRequest): Promise<{ name: string; uuid: string; message: string }> => {
    const response = await wrapApiCall<ApiResponse>(apiClient.post('/domains', data, {
      params: { connection: connectionId }
    }));
    return response.data as { name: string; uuid: string; message: string };
  },

  // 删除虚拟机
  deleteDomain: async (connectionId: string, domainName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.delete('/domain', {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 启动虚拟机
  startDomain: async (connectionId: string, domainName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/domain/start', null, {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 关闭虚拟机
  shutdownDomain: async (connectionId: string, domainName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/domain/shutdown', null, {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 强制停止虚拟机
  forceStopDomain: async (connectionId: string, domainName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/domain/force-stop', null, {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 重启虚拟机
  rebootDomain: async (connectionId: string, domainName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/domain/reboot', null, {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 挂起虚拟机
  suspendDomain: async (connectionId: string, domainName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/domain/suspend', null, {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 恢复虚拟机
  resumeDomain: async (connectionId: string, domainName: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/domain/resume', null, {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 设置虚拟机内存
  setDomainMemory: async (connectionId: string, domainName: string, memory: number): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.put('/domain/memory', { memory }, {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 设置虚拟机CPU
  setDomainVCPUs: async (connectionId: string, domainName: string, vcpus: number): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.put('/domain/vcpus', { vcpus }, {
      params: { 
        connection: connectionId,
        name: domainName 
      }
    }));
  },

  // 获取虚拟机状态
  getDomainStatus: async (connectionId: string, domainName: string): Promise<{ state: string }> => {
    const response = await wrapApiCall<{ state: string }>(apiClient.get('/domain/status', {
      params: {
        connection: connectionId,
        name: domainName
      }
    }));
    return response;
  },

  // 获取虚拟机统计信息
  getDomainStats: async (connectionId: string, domainName: string): Promise<any> => {
    const response = await wrapApiCall<any>(apiClient.get('/domain/stats', {
      params: {
        connection: connectionId,
        name: domainName
      }
    }));
    return response;
  },

  // 获取虚拟机XML配置
  getDomainXML: async (connectionId: string, domainName: string): Promise<string> => {
    const response = await apiClient.get('/domain/xml', {
      params: { 
        connection: connectionId,
        name: domainName 
      },
      responseType: 'text',
    });
    return response.data;
  },

  // 生成虚拟机XML模板
  generateDomainXML: async (template: CreateDomainRequest): Promise<string> => {
    const response = await apiClient.post('/templates/generate', template, {
      responseType: 'text',
    });
    return response.data;
  },
};

export default domainService;